﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoyoEden;
using System.Collections.Specialized;
using System.Web.UI.HtmlControls;
using CoyoEden.Models;

/// <summary>
/// A popup menu control,using jquery.droppy.js
/// </summary>
public partial class widgets_CategoryMenu_widget : WidgetBase
{

	#region base implementation
	protected override void OnLoad(EventArgs e) {
		//css and js used by this widget
		Utils.AddJavaScriptInclude(Utils.RelativeWebRoot + "assets/js/plugins/jquery.droppy.js", Page, true);
		Utils.AddStylesheetInclude(Utils.RelativeWebRoot + "assets/css/droppy.css",Page, false);
		base.OnLoad(e);
	}
	public override string Name {
		get { return "CategoryMenu"; }
	}

	public override bool IsEditable {
		get { return true; }
	}

	public override void LoadWidget() {
		LoadSettings();
		categoryMenu.Categories = SELECTEDCATEGORIES;
	}
	#endregion

	#region private fields
	private List<Category> SELECTEDCATEGORIES;
	#endregion

	#region helper methods
	private void LoadSettings() {
		StringDictionary settings = GetSettings();
		if (settings.ContainsKey("clist")) {
			string tempStr = settings["clist"];
			string[] categoryIDs = tempStr.Split('|');
			SELECTEDCATEGORIES = new List<Category>();
			categoryIDs.ToList().ForEach(cID => {
				var C = Category.GetCategory(Convert.ToInt32(cID));
				if (C != null)
				{
					SELECTEDCATEGORIES.Add(C);
				}
			});
		}
	}
	#endregion
}
